package sui.utils.compress;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

/**
 * <p>
 * Description: 压缩工具类
 * </p>
 * @author XQF SUI
 * @Created: 2014-6-3 上午10:20:29
 * @version 1.0
 */
public final class CompressionUtils {  
  
    private static final int BUFFER_SIZE = 4 * 1024;  
  
    /**
     * <p>
     * Description：byte[]压缩
     * </p>
     * @author XQF SUI
     * @created 2014-6-3 上午10:21:06
     * @param data	数据
     * @param level	等级
     * @return
     * @throws IOException
     */
    public static byte[] compress(byte[] data, Level level) throws IOException {  
  
        Deflater deflater = new Deflater();  
        // set compression level  
        deflater.setLevel(level.getLevel());  
        deflater.setInput(data);  
  
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);  
  
        deflater.finish();  
        byte[] buffer = new byte[BUFFER_SIZE];  
        while (!deflater.finished()) {  
            int count = deflater.deflate(buffer); // returns the generated  
                                                  // code... index  
            outputStream.write(buffer, 0, count);  
        }  
        byte[] output = outputStream.toByteArray();  
        outputStream.close();  
        return output;  
    }
    
    /**
     * <p>
     * Description：压缩String数据
     * </p>
     * @author XQF SUI
     * @created 2014-6-3 上午10:21:49
     * @param data
     * @param level
     * @return
     * @throws UnsupportedEncodingException
     * @throws IOException
     */
    public static byte[] compress(String data, Level level) throws UnsupportedEncodingException, IOException {
		return compress(data.getBytes(), level);
    }
    
    
    /**
     * <p>
     * Description：压缩
     * </p>
     * @author XQF SUI
     * @created 2014-6-3 上午10:22:23
     * @param data
     * @param level
     * @return
     * @throws UnsupportedEncodingException
     * @throws IOException
     */
    public static String compressToString(byte[] b, Level level) throws UnsupportedEncodingException, IOException {
    	return byteToString(compress(b, level));
    }
    
    /**
     * <p>
     * Description：压缩
     * </p>
     * @author XQF SUI
     * @created 2014-6-3 上午10:22:58
     * @param data
     * @param level
     * @return
     * @throws UnsupportedEncodingException
     * @throws IOException
     */
    public static String compressToString(String data, Level level) throws UnsupportedEncodingException, IOException {
    	return byteToString(compress(data.getBytes(), level));
    }
    
    /**
     * <p>
     * Description：解压
     * </p>
     * @author XQF SUI
     * @created 2014-6-3 上午10:23:59
     * @param data
     * @return
     * @throws IOException
     * @throws DataFormatException
     */
    public static byte[] decompress(byte[] data) throws IOException, DataFormatException {  
  
        Inflater inflater = new Inflater();  
        inflater.setInput(data);  
  
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);  
        byte[] buffer = new byte[BUFFER_SIZE];  
        while (!inflater.finished()) {  
            int count = inflater.inflate(buffer);  
            outputStream.write(buffer, 0, count);  
        }  
        byte[] output = outputStream.toByteArray();  
        outputStream.close();  
        return output;  
    }
    
    /**
     * <p>
     * Description：解压
     * </p>
     * @author XQF SUI
     * @created 2014-6-3 上午10:25:12
     * @param data
     * @return
     * @throws IOException
     * @throws DataFormatException
     */
    public static byte[] decompress(String data) throws IOException, DataFormatException {  
        return decompress(stringToByte(data));  
    }
    
    /**
     * <p>
     * Description：解压
     * </p>
     * @author XQF SUI
     * @created 2014-6-3 上午10:25:12
     * @param data
     * @return
     * @throws IOException
     * @throws DataFormatException
     */
    public static String decompressToString(byte[] data) throws IOException, DataFormatException {  
        return byteToString(decompress(data));  
    }
    
    /**
     * <p>
     * Description：解压
     * </p>
     * @author XQF SUI
     * @created 2016年11月14日 下午5:41:20
     * @param data
     * @return
     * @throws IOException
     * @throws DataFormatException
     */
    public static String decompressToString(String data) throws IOException, DataFormatException {  
        return byteToString(decompress(stringToByte(data)));  
    }
    
    public static String byteToString(byte[] b) throws UnsupportedEncodingException {
    	return new String(b, "ISO8859-1");
    }
    
    public static byte[] stringToByte(String str) throws UnsupportedEncodingException {
    	return str.getBytes("ISO8859-1");
    }
    
    /** 
     * Compression level 
     */  
    public static enum Level {  
  
        /** 
         * Compression level for no compression. 
         */  
        NO_COMPRESSION(0),  
  
        /** 
         * Compression level for fastest compression. 
         */  
        BEST_SPEED(1),  
  
        /** 
         * Compression level for best compression. 
         */  
        BEST_COMPRESSION(9),  
  
        /** 
         * Default compression level. 
         */  
        DEFAULT_COMPRESSION(-1);  
  
        private int level;  
  
        Level(int level) {  
            this.level = level;  
        }  
        public int getLevel() {  
            return level;  
        }  
    } 
}  